package com.augurit.common.calendar.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.util.PageUtils;
import com.augurit.common.util.HqlUtils;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.Page;
import com.augurit.common.calendar.convert.SysCalSharedConvertor;
import com.augurit.common.calendar.convert.SysCalendarConvertor;
import com.augurit.common.dao.SysCalSharedDao;
import com.augurit.common.entity.SysCalShared;
import com.augurit.common.entity.SysCalendar;
import com.augurit.common.calendar.service.ISysCalSharedService;
import com.augurit.common.calendar.web.form.SysCalSharedForm;
import com.augurit.common.calendar.web.form.SysCalendarForm;

@Service
@Transactional
public class SysCalSharedServiceImpl implements ISysCalSharedService{

	@Autowired
	private SysCalSharedDao sysCalSharedDao;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public SysCalSharedForm get(Long id) {
		SysCalShared entity = sysCalSharedDao.get(id);
		return SysCalSharedConvertor.convertVoToForm(entity);
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		sysCalSharedDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(SysCalSharedForm... forms) {
		if(forms != null)
			for(SysCalSharedForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(SysCalSharedForm form){
		
		if(form != null){
			SysCalShared entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = sysCalSharedDao.get(form.getId());
			}else{
				entity = new SysCalShared();
			}
			
			//属性值转换
			SysCalSharedConvertor.convertFormToVo(form, entity);
			
			//保存
			sysCalSharedDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<SysCalSharedForm> search(Page<SysCalSharedForm> page, SysCalSharedForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from SysCalShared ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		sysCalSharedDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<SysCalSharedForm> list = SysCalSharedConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<SysCalSharedForm> search(Page<SysCalSharedForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<SysCalShared> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		sysCalSharedDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<SysCalSharedForm> list = SysCalSharedConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	public List<SysCalSharedForm> getByShareType(Long eventId, String shareType){
		if(eventId != null){
			String hql = "select share, user.userName from SysCalShared share, OmUser user where user.userId = share.shareUserId and share.eventId = ? and share.shareType = ?";
			List<Object[]> objs = sysCalSharedDao.find(hql, eventId, shareType);
			return SysCalSharedConvertor.covertObjsToFormList(objs);
		}
		return null;
		
	}
	
	/**
	 * 获取某一事件的所有编辑者
	 * @param eventId
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<SysCalSharedForm> getEditors(Long eventId){
		return this.getByShareType(eventId, SysCalSharedForm.WRITE_SHARE_TYPE);
	}
	
	/**
	 * 获取某一事件的所有读者
	 * @param eventId
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<SysCalSharedForm> getReaders(Long eventId){
		return this.getByShareType(eventId, SysCalSharedForm.READ_SHARE_TYPE);
	}
	
	/**
	 * 获取某一事件的所以共享者
	 *  @param eventId
	 *  @return
	 */
	public List<SysCalSharedForm> getSharedUsers(Long eventId){
		if(eventId != null){
			String hql = "select share, user.userName from SysCalShared share, OmUser user where user.userId = share.shareUserId and share.eventId = ?";
			List<Object[]> objs = sysCalSharedDao.find(hql, eventId);
			return SysCalSharedConvertor.covertObjsToFormList(objs);
		}
		return null;
	}
	
	public void save(Long eventId, Long[] shareUserIds, String[] shareTypes){
		if(eventId != null && shareUserIds != null && shareTypes != null && shareUserIds.length == shareTypes.length){
			List<SysCalShared> entities = sysCalSharedDao.findBy("eventId", eventId);
			if(entities != null && entities.size() > 0)
				sysCalSharedDao.delete(entities);
			
			for(int i = 0; i < shareUserIds.length; i++){
				this.save(eventId, shareUserIds[i], shareTypes[i]);
			}
		}
	}
	
	public void save(Long eventId, Long shareUserId, String shareType){
		if(eventId != null && shareUserId != null && (shareType.equals("W") || shareType.equals("R"))){
			SysCalShared entity = new SysCalShared();
			entity.setEventId(eventId);
			entity.setShareUserId(shareUserId);
			entity.setShareType(shareType);
			sysCalSharedDao.save(entity);
		}
	}
}
